package com.company.cloud.page.modules.kit;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.company.cloud.common.bean.DataMapEntity;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TemplateKit {

    private static Engine _engine = null;

    public static Engine getEngine() {
        if (_engine != null) {
            return _engine;
        }
        _engine = Engine.use();
        _engine.setDevMode(true);
        _engine.setToClassPathSourceFactory();
        _engine.setBaseTemplatePath("/view/");

//        Engine.setFastMode(true);
        return _engine;
    }

    public static String getContent(String templateContent, DataMapEntity data) {
        if(StrUtil.isEmpty(templateContent)){
            return "";
        }
        if(data==null){
            data=new DataMapEntity();
        }

        Engine engine = getEngine();
        Template template = engine.getTemplateByString(templateContent);
        String result = template.renderToString(data);
        return result;
    }


    public static String getContent(String templateContent, Map<String, Object> data) {
        if(StrUtil.isEmpty(templateContent)){
            return "";
        }
        if(data==null){
            data=new HashMap<>();
        }
        Engine engine=getEngine();
        Template template = engine.getTemplateByString(templateContent);
        String result = template.renderToString(data);
        return result;
    }


    public static  Map<String,String> getPageTempParse(String text){
        Map<String,String> data=new HashMap<>();
        String pattern="(\\(ZJ_)(.*?)(\\?\\?)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(text);
        Set<String> pageComponentList=new HashSet<>();
        while (m.find()) {
            System.out.println("Found value: " + "ZJ_"+m.group(2));
            String _pageComponent=m.group(2);
            if(StrUtil.isNotEmpty(_pageComponent)){
                String pageComponent= StrUtil.format("ZJ_{}",_pageComponent);
                data.put(pageComponent,pageComponent.substring(0, pageComponent.lastIndexOf("_")));
            }
        }
        return data;
    }

    public static  String removePageComponentTemp(String text,String pageComponentNo){
        int idx=text.indexOf(pageComponentNo);
        if(idx<=0){
            return null;
        }
        String t1=text.substring(0,idx);
        String t2=text.substring(idx);

        String t11=t1.substring(0,t1.lastIndexOf("#"));
        String t22=t2.substring(t2.indexOf(")")+1);

        String text2=StrUtil.format("{}{}",t11,t22);

        System.out.println("+++++移除后的页面内容:"+text2);

        return text2;
    }

    public static String getDevPageComponentTemp(String html,String pageComponentNo){

        Document doc = Jsoup.parse(html);
        Element ele=doc.body().child(0);
        if(ele==null){
            return html;
        }
        ele.addClass("data-com-editor");
        ele.attr("data-editor-id",pageComponentNo);

        String _html=ele.outerHtml();
        System.out.println("+++++++++dev html 内容++++++++++++"+_html);
        return _html;
    }



    public static void main(String[] args) {


        String s="<html><body>解放路口的撒娇发空间#(ZJ_WJJ_1 ??)</body></html>";

        Map<String,Object> pageData=new HashMap<>();
        pageData.put("ZJ_WJJ_1","<div id=\"test\">title1111</div>");

         String sss=TemplateKit.getContent(s,pageData);

        System.out.println(sss);





    }

}